<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Cache;

class Article extends BasicAdmin
{
    public $table = 'Article';

    /**
     * 数据列表展示
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * 获取列表数据
     * @return \think\response\Json
     */
    public function getList(){
        $get_datas = $this->request->get();
        $where = [];

        $db = Db::name($this->table);

        if(isset($get_datas['name']) && $get_datas['name']) {
            $where[] = ['name', 'like', '%'.$get_datas['name'].'%'];
        }

        $res  = DataService::getData($db, $get_datas['limit']??10, $where);

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return json($data);
    }

    /**
     * 添加信息
     * @return \think\response\View
     */
    public function create()
    {
        $res = Db::name('ArticleCategory')->where('status', 1)->select();
        $data = formatData($res);
         $tags = Db::table('tag')->where('status', 1)->select();
        return view('', ['parents' => $data,'tags' => $tags]);
    }

    /**
     * 保存信息
     * @return \think\response\Json
     */
    public function store()
    {
        $data = $this->request->post();

        $title = $data['name'];
        $is = Db::name($this->table)->where('name',$title)->find();
        if($is){
            $data = [
                'code' => 0,
                'msg'   => '标题重复啦',
            ];
            return json($data);
        }

        $data['create_time'] = date('Y-m-d H:i:s');
        $tags = $data['tags']??[];
        unset($data['tags']);
        $article_id = DataService::save(Db::name($this->table), $data, 'id', []);
        $article_tags = [];
        foreach ($tags as $k => $v){
            if($v){
                $article_tags[] = ['tag_id'=>$v, 'article_id' => $article_id];
            }
        }

        Db::table('article_tag')->insertAll($article_tags);

        $data = [
            'code' => 200,
            'msg'   => '添加成功',
        ];
        return json($data);
    }

    /**
     * 修改信息页面
     * @param int $id  信息的主键id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        $res = Db::name($this->table)->where('id',$id)->find();
        $data['data'] = $res;
        $res = Db::name('ArticleCategory')->where('status', 1)->select();
        //$datas = formatData($res);
        $data['parents'] = formatData($res);
        $is_checked = Db::table('article_tag')->where('article_id', $id)->column('tag_id');
        $tags = Db::table('tag')->where('status', 1)->select();
        $data['tags'] = $tags;
        $data['is_checked'] = $is_checked;
        return view('',$data);
    }

    /**
     * 保存修改信息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update()
    {
        $data = $this->request->post();
        $id = $data['id'];
        $title = $data['name'];
        $is = Db::name($this->table)->where('name',$title)->where('id','<>',$id)->find();
        if($is){
            $data = [
                'code' => 0,
                'msg'   => '标题重复啦',
            ];
            return json($data);
        }
        if(isset($data['tags']) && $data['tags']){
            $tags = $data['tags'];
            unset($data['tags']);
        }else{
            $tags = [];
        }


        $res = Db::name($this->table)->where('id',$id)->update($data);

        $is_checked = Db::table('article_tag')->where('article_id', $id)->column('tag_id');

        $add = array_diff($tags,$is_checked);
        $delete = array_diff($is_checked,$tags);
        $is_update = 0;
        if(count($add)>0){
            $article_tags = [];
            foreach ($add as $k => $v){
                if($v){
                    $article_tags[] = ['tag_id'=>$v, 'article_id' => $id];
                }
            }

            $res = Db::table('article_tag')->insertAll($article_tags);
            if($res){
                $is_update++;
            }
        }

        if(count($delete)>0){
            $res = Db::table('article_tag')->where('article_id',$id)->whereIn('tag_id',$delete)->delete();
            if($res){
                $is_update++;
            }
        }

        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '更新成功',
            ];
        } else {
            if($is_update){
                $data = [
                    'code' => 200,
                    'msg'   => '更新成功',
                ];
            } else {
                $data = [
                    'code' => 0,
                    'msg'   => '更新失败',
                ];
            }
        }

        return json($data);
    }

    /**
     * 删除信息 ids 【批量删除】
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function destroy()
    {
        $ids = $this->request->post();
        if (empty($ids['ids'])){
            return json(['code'=>0,'msg'=>'请选择删除项']);
        }
        $res = Db::name($this->table)->whereIn('id', $ids['ids'])->delete();
        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '删除成功',
            ];
        } else {
            $data = [
                'code' => 0,
                'msg'   => '删除失败',
            ];
        }

        return json($data);
    }

    /**
     * 设置置顶信息 ids
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setTop()
    {
        $data = $this->request->post();
        if (empty($data['ids'])){
            return json(['code'=>0,'msg'=>'请选择数据项']);
        }
        $update['top'] = $data['top'];
        $res = Db::name($this->table)->whereIn('id', $data['ids'])->update($update);
        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '成功',
            ];
        } else {
            $data = [
                'code' => 0,
                'msg'   => '失败',
            ];
        }

        return json($data);
    }


    public function uploads(){
        return uploads();
    }

    public function updateAct(){
        $data = $this->request->post();
        $res = Db::name($this->table)->where('id', $data['id'])->update([$data['act'] => $data['value']]);
        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '更新成功',
            ];
        } else {
            $data = [
                'code' => 0,
                'msg'   => '更新失败',
            ];
        }
        return json($data);
    }


    public function export($ids){

        $db = Db::name('Article');
        if(!$ids){
            $this->error('选择导出数据 ');
        }

//        if($start_time) {
//            $db->where('create_time','>=',$start_time);
//        }else{
//            $this->error('选择时间');
//        }
//
//        if($end_time) {
//            $db->where('create_time','<=',$end_time);
//        }else{
//            $this->error('选择时间');
//
//        }

        $res = $db->whereIn('id',$ids)->select();

        $xlsName  = "article";
        $xlsCell  = array(
            array('name','名称'),
            array('sort','电话'),
            array('status','金额'),
            array('content','df'),
            array('top','是否有房')
        );

        exportExcel($xlsName,$xlsCell,$res);

    }


}
